<?php
function find_child($menu_id)
{
	$q1=mysql_query("select * from menu where menu.parent='$menu_id' and menu.show='1' order by menu.rank asc");
	$num=mysql_num_rows($q1);
	if($num >0)
	{
		return 1;
	}
	else
		return 0;
}
function find_parent($menu_id)
{
	$q1=mysql_query("select menu.parent from menu where menu.id='$menu_id'");
	$p=mysql_fetch_row($q1);
	//$q1=mysql_query("select menu.parent from menu where menu.id='$menu_id'");
	//$num=mysql_num_rows($q1);
	if($p != null)
	{
		return $p[0];
	}
	else
		return null;
}
function find_level($menu_id)
{
	//$q1=mysql_query("select menu.parent from menu where menu.id='$menu_id'");
	//$p=mysql_fetch_row($q1);
	//$q1=mysql_query("select menu.parent from menu where menu.id='$menu_id'");
	//$num=mysql_num_rows($q1);
	$id=$menu_id;
	$dem=0;
	$temp;
	while ($temp != 1)
	{
		if (find_parent($id) != 0 )
		{
			//echo "vo";echo "</br>";
			$temp= find_parent($id);
			$id=$temp;
			$dem++;
		}
	}
	
	
		return $dem;
}
function find_name($menu_id)
{
	$q1=mysql_query("select menu.name from menu where menu.id='$menu_id' ");
	$r=mysql_fetch_row($q1);
	return $r[0];
}
function list_level($menu_id)
{
	$id=$menu_id;
	$dem=0;
	$temp;
	//$q1=mysql_query("select * from menu where menu.parent='$parent_id' order by menu.rank asc ");
	$menu= array($menu_id);
	while ($temp != 1)
	{
		if (find_parent($id) != 0 )
		{
			//echo "vo";echo "</br>";
			$temp= find_parent($id);
			$id=$temp;
			//$menu[] = array('id' => $temp);
			array_unshift($menu, $temp);
			//echo "$temp";echo "</br>";
			$dem++;
		}
	}
	
	
		return $menu;
}
function last_child($parent_id)
{
	$q1=mysql_query("select menu.id from menu where menu.parent='$parent_id' order by menu.id desc limit 0,1");
	$r=mysql_fetch_row($q1);
	
		return $r[0];
}
function list_child($parent_id)
{
	$q1=mysql_query("select * from menu where menu.parent='$parent_id' order by menu.rank asc ");
	//$r=mysql_fetch_row($q1);
	
	while($r=mysql_fetch_array($q1))
		{	
			//echo $r['name'].'</br>';
			$menu[] = array('id' => $r['id'], 'name' => $r['name'], 'parent' => $r['parent'], 'rank' => $r['rank'],'show' => $r['show'],'murl' => $r['murl']);
			//echo "123";
		}
	return $menu;	
}
function recursive_menu($arrData, $parent = 0, $level = 0, &$result) {
    if (count($arrData) > 0) {
        foreach ($arrData as $key => $val) {
            if ($parent == $val['parent']) {
                $val['level'] = $level;
                $result[] = $val;
                $_parent = $val['id'];
                unset($arrData[$key]);
                recursive_menu($arrData, $_parent, $level + 1, $result);
            }
        }
    }
	}
function list_no_child($parent_id)
{
		$q=mysql_query("select * from menu order by menu.id asc ");
		while($r=mysql_fetch_array($q))
		{	
			//echo $r['name'].'</br>';
			$menu[] = array('id' => $r['id'], 'name' => $r['name'], 'parent' => $r['parent'], 'rank' => $r['rank'],'show' => $r['show']);
			//echo "123";
		}
	recursive_menu($menu, $parent_id, 0, $result2);
	$list='';
	foreach ($result2 as $key => $val) {
		$menu_id=$val['id'];
		if (find_child($menu_id) == 1 )
			continue;
		if (find_child($menu_id) == 0 )	{
			$menu2[] = array('id' => $menu_id);
			
		}	
	}
	
	
	return $menu2;	
}
function find_max_child($parent_id)
{
	$q1=mysql_query("select menu.rank from menu where menu.parent='$parent_id' order by menu.rank asc ");
	//$r=mysql_fetch_row($q1);
	$max=0;
	while($r=mysql_fetch_array($q1))
		{	
			if ($r[0]>= $max)
				$max=$r[0];
		}
	return $max;	
}
function find_total_level()
{
	$q=mysql_query("select menu.id from menu ");
	//$p=mysql_fetch_row($q1);
	//$q1=mysql_query("select menu.parent from menu where menu.id='$menu_id'");
	//$num=mysql_num_rows($q1);
	$total=0;
	$dem=0;
	
	while($r=mysql_fetch_array($q))
	{	
		
		$temp=find_level($r[0]);
		if ( $total < $temp )
		{
			$total=$temp;
		}
		
		//echo $r[0];
	}
	
	
		return $total;
}
?>
